//----------------------------------------------------------------------------
// File       : tTask_Core.c
// Function   : Provide the main function of task scheduler.
// Description: To be done.
// Version    : V1.00
// Author     : lucien
// Date       : 09.18/19
// History    :  No.    When       Who         What
//               1    09.18/19    lucien      Create
//----------------------------------------------------------------------------



#include <Source/System/inc/Task_Core.h>




static Task_t _TaskList[] = {null};


//Event ticks
static uint16_t _TaskTail = 0;

//Timer ticks
static uint32_t _timer_ticks = 0;

//----------------------------------------------------------------------------
// Name       : uint8 tTask_Init(PF_TIMER_SRC pfSysTm, PF_POLL pfPoll)
// Function   : Init MOE
// Input      : PF_TIMER_SRC pfSysTm   Free running system clock in ms
//              PF_POLL      pfPoll    The function to be polled(could be NULL)
// Output:    : None
// Return     : SW_OK   Successful.
//              SW_ERR  Failed.
// Description: 1. Check system clock function parameter
//              2. Save poll function (even it is NULL)
//              3. Init timer
//              4. Init HAL
//              5. Init Message
//              6. Init event processing
//              7. Init all tasks.
//---------------------------------------------------------------------------
uint8_t Task_Init(void)
{

    // Init timer
    // tTask_Timer_Init();

    // Init all tasks


    return SW_OK;
}



/******************************************************************************
* Name       : void tTask_Run_System(void)
* Function   : The main function to schedule tasks.
* Input      : None
* Output:    : None
* Return     : None
* Description: 1. Process timers
*              2. Do poll function if available
*              3. Get event and process task
* Version    : V1.00
* Author     : Ian
* Date       : 28th Apr 2016
******************************************************************************/
#if DISPATCH
void Task_Run(uint16_t event)
{
    uint16_t i;

    for(i = 0; i < _TaskTail; i++)
    {
        if(_TaskList[i].status == TASK_RUN)
        {
            _TaskList[i].cb(_TaskList[i].event);
        }

    }
}


#else


void Task_Run(void)
{
    uint16_t i;

    for(i = 0; i < _TaskTail; i++)
    {
        if(_TaskList[i].status == TASK_RUN)
        {
            _TaskList[i].cb();
        }

    }
}

#endif


//----------------------------------------------------------------------------
// Name       : void tTask_Run_System(void)
//-Function   : The main function to schedule tasks.
//-Input      : None
//-Output:    : None
//-Return     : None
//-Description: 1. Process timers
//-             2. Do poll function if available
//-             3. Get event and process task
//----------------------------------------------------------------------------
TaskSt_t Task_Add(cb_t pFunc)
{
    uint16_t i;
    TaskSt_t _st_ = TASK_OK;
    for(i = 0; i < _TaskTail; i++)
    {
        if(_TaskList[_TaskTail].cb == pFunc)
        {
            //_st_ = TASK_EXIST;
            return TASK_EXIST;
        }
    }

    _TaskList[_TaskTail].cb = pFunc;
    _TaskList[_TaskTail].status = TASK_RUN;
    _TaskList[_TaskTail].timeout = 0;
    _TaskList[_TaskTail].repeat = 0;
    _TaskTail++;
    _st_ = TASK_OK;

    if(_TaskTail >= MAX_HANDLE_QUEUE)
    {
        _st_ = TASK_FULL;;
    }
    return _st_;
}


//----------------------------------------------------------------------------
// Name       : void tTask_Run_System(void)
//-Function   : The main function to schedule tasks.
//-Input      : None
//-Output:    : None
//-Return     : None
//-Description: 1. Process timers
//-             2. Do poll function if available
//-             3. Get event and process task
//----------------------------------------------------------------------------
TaskSt_t Task_Del(cb_t pFunc)
{
    uint16_t i;

    for(i = 0; i < _TaskTail; i++)
    {
        if(_TaskList[_TaskTail].cb == pFunc)
        {
            _TaskList[_TaskTail].event = null;
            _TaskList[_TaskTail].status = null;
            _TaskList[_TaskTail].timeout = null;
            _TaskList[_TaskTail].repeat = null;
            _TaskTail--;
            return TASK_EMPTY;
        }
    }

    return TASK_NOT;
}


//----------------------------------------------------------------------------
// Name       : void tTask_Run_System(void)
//-Function   : The main function to schedule tasks.
//-Input      : None
//-Output:    : None
//-Return     : None
//-Description: 1. Process timers
//-             2. Do poll function if available
//-             3. Get event and process task
//----------------------------------------------------------------------------
void TaskTickHandler(void)
{
    uint16_t i;

    for(i = 0; i < _TaskTail; i++)
    {
        if(_TaskList[i].status == TASK_WAILT)
        {

            if(_timer_ticks >= _TaskList[i].timeout)
            {
                _TaskList[i].cb();
            }
            else
            {
                _TaskList[i].timeout = _timer_ticks + _TaskList[i].repeat;
            }
        }

    }
}


//----------------------------------------------------------------------------
// Name       : void tTask_Run_System(void)
//-Function   : The main function to schedule tasks.
//-Input      : None
//-Output:    : None
//-Return     : None
//-Description: 1. Process timers
//-             2. Do poll function if available
//-             3. Get event and process task
//----------------------------------------------------------------------------
TaskSt_t TimerTask_Add(cb_t pFunc, uint32_t timeout, uint32_t repeat)
{
    uint16_t i;
    TaskSt_t _st_ = TASK_OK;

    for(i = 0; i < _TaskTail; i++)
    {
        if(_TaskList[_TaskTail].cb == pFunc)
        {
            //_st_ = TASK_EXIST;
            return TASK_EXIST;
        }
    }

    _TaskList[_TaskTail].cb = pFunc;
    _TaskList[_TaskTail].status = TASK_WAILT;
    _TaskList[_TaskTail].timeout = _timer_ticks + repeat;
    _TaskList[_TaskTail].repeat = repeat;
    _TaskTail++;
    _st_ = TASK_OK;

    if(_TaskTail >= MAX_HANDLE_QUEUE)
    {
        _st_ = TASK_FULL;;
    }
    return _st_;
}


//----------------------------------------------------------------------------
// Name       : void tTask_Run_System(void)
//-Function   : The main function to schedule tasks.
//-Input      : None
//-Output:    : None
//-Return     : None
//-Description: 1. Process timers
//-             2. Do poll function if available
//-             3. Get event and process task
//----------------------------------------------------------------------------
void TaskTick(void)
{
    _timer_ticks ++;
}


// End of file
